using FytSoa.Application.Hr.Param;
using FytSoa.Domain.Hr;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Hr;

/// <summary>
/// 排班表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v8")]
public class HrRowWorkService : IApplicationService 
{
    private readonly SugarRepository<HrRowWork> _thisRepository;
    private readonly SugarRepository<HrGlobalWork> _globalWorkRepository;
    public HrRowWorkService(SugarRepository<HrRowWork> thisRepository
        ,SugarRepository<HrGlobalWork> globalWorkRepository)
    {
        _thisRepository = thisRepository;
        _globalWorkRepository = globalWorkRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<HrRowWorkDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<HrRowWorkDto>>();
    }
    
    /// <summary>
    /// 根据用户查询全局设置以及自定义设置排班内容
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<HrRowWorkEmployeeDto> GetEmployeeWorkAsync(HrRowWorkSearchParam param)
    {
        var rowWork = await _thisRepository
            .AsQueryable()
            .Includes(m=>m.ShiftsObj)
            .Where(m => m.EmployeeId == param.EmployeeId
                        && m.Time.ToString("yyyy-MM") ==
                        param.Time.ToString("yyyy-MM"))
            .ToListAsync();
        var global = await _globalWorkRepository
            .AsQueryable()
            .Includes(m=>m.EmployeeObj)
            .Includes(m=>m.ShiftsObj)
            .FirstAsync(m => m.EmployeeId == param.EmployeeId);
        return new HrRowWorkEmployeeDto()
        {
            GlobalWork = global.Adapt<HrGlobalWorkDto>(),
            RowWork = rowWork.Adapt<List<HrRowWorkDto>>()
        };
    }


    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<HrRowWorkDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<HrRowWorkDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task AddAsync(HrRowWorkDto model)
    {
        await _thisRepository.DeleteAsync(m => m.EmployeeId == model.EmployeeId && m.Time.ToString("yyyy-MM-dd")==model.Time.ToString("yyyy-MM-dd"));
        await _thisRepository.InsertAsync(model.Adapt<HrRowWork>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(HrRowWorkDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<HrRowWork>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
